/**
 * 系统业务日志管理设置
 * 
 * @version 1.0.0
 * @author yangyibao
 */
var logDataTable;

$(function () {
//	 初始化时间插件
	$("#logSearchForm #logCrtdtSearch").datepicker({
		language : 'zh-CN',
		CustomFormat : "yyyy-mm-dd",
		minView : "month",
		changeYear : true,
		yearRange : "-200:+0",
		timepicker : false,
		todayBtn : true,
		autoclose : true
	});
	
	logDataTable = $('#logDataTable').DataTable({
		   "bProcessing": true,
           "bLengthChange":true,
           "bFilter": false,//无搜索 
           "bServerSide": true,//服务端数据处理
           "iDisplayLength":10,//默认数据条数
           "bSort": false,//排序功能
           "pagingType":"full_numbers",
           "sAjaxSource": "../system/log/sysLogs",//后端服务地址
           "fnServerParams" : function(aDataSet) {
					aDataSet.push(
							{"name" : "userNo","value" :$("#logSearchForm #userNo").val()},
							{"name" : "logCrtdt","value":$("#logSearchForm #logCrtdtSearch").val()},
							{"name" : "logTime","value":$("#logSearchForm #logTime").val()},
							{"name" : "logIserror","value":$("#logSearchForm #logIserror").val()});},
           "fbServerData":function(sSource,aDataSet){$.ajax({"dataType":"json","type":"GET","url":sSource,"data":aDataSet});},
           "aoColumns":[{"mDataProp":"userNo"},{"mDataProp":"userName"},{"mDataProp":"operateType"},{"mDataProp":"logUrl"},{"mDataProp":"operateCnt"},{"mDataProp":"logTime"},{"mDataProp":"logIserror"},{"mDataProp":"logCrtdt"},{"mDataProp":"logId"}],
           "aoColumnDefs":[
                           {
                        	   "aTargets":[6],
                        	   "mRender":function(data,type,full){
                        		   var str = "";
                        		   if(data == "1"){
                        			   str = "<font style='color:red'>错误</font>";
                        		   }else{
                        			   str = "<font style='color:green'>正常</font>";
                        		   }
                        		   return str;
                        	   }
                           },{
                        	   "aTargets":[8],
                        	   "mRender":function(data,type,full){
                        		   var dltBtn = "<button class='btn btn-lightpink btn-outline m-wrap' data-id='"+ data + "' id='dltBtn'><i class='icon-remove'></i></button>&nbsp;&nbsp;";
                        		   var detailBtn="<button class='btn btn-white btn-outline m-wrap' data-json='"+JSON.stringify(full)+"' data-id='"+data+"' id='detailDataBtn' style='font-size:12px;line-height:15px;'><i class='icon-search'></i></button>"
                        		   return detailBtn;
                        	   }
                           }
                          ],
           "oLanguage":{"sUrl": "../resources/assets/plugins/dataTables/txt/page.txt"},
            "initComplete":function () {
                $('#logDataTable_length').find('select').selectpicker('render');
                $('#logDataTable_length').find('select').selectpicker('refresh');
                $('#logDataTable_length').find('.btn-group').css('width','auto');
                $('#logDataTable_length').find('.btn').css('background-color','#fff');
                $('#logDataTable_length').find('.btn').css('border','#d8d8d8 solid 1px')
            }
    });
	
	//删除业务日志信息
	$('#logDataTable tbody').on('click','#dltBtn',function(){
		var logId = $(this).attr("data-id");
		confirm("删除日志?",function(isConfirm){if (isConfirm) {$.post("/mcode/system/log/sysLogDlt",{logId:logId},function(data){if(data.flg){logDataTable.ajax.reload();swal("", "当前操作成功 :)", "success");}else swal("失败 :)", "当前操作失败 :)", "error");});} else  swal("取消", "您的操作已经取消 :)", "error");});
	});
	
	//详情业务日志信息
	$('#logDataTable tbody').on('click','#detailDataBtn',function(){
		var data_id = $(this).attr("data-id");
		$.post("../system/log/queryLogByPk",{"logId":data_id}, function(data){
			for(k in data){
				var ele = $("#detaliInfoDiv #" + k);
				if(ele.length>0){
					if(k == "logErrormsg"){
						ele.val(data[k]);
					}else{
						ele.html(data[k]);
					}
				}
			}
			$("#detaliInfoDiv #detaliParamDiv").html(getParamInfo(data.operateCnt));
			$("#detailDlg").modal("show");
		})
	});
	
	function appendHtmp(val1,val2){
		var sub = '<tr><td style = "width:275px" class="bold font-blue-hoki">'+val1+'</td>';
		var subval = '<td style = "width:275px">'+val2+'</td></tr>';
		return sub + subval;
	}
	
	function getParamInfo(str){
		var html = "";
		if(str != undefined && str.indexOf(",">0)){
			var node = str.split(",");
			for(var i=0;i<node.length;i++){
				var tmpnode = node[i];
				if(tmpnode.indexOf(":")>0){
					var tmp = tmpnode.split(":");
					var index = tmpnode.indexOf(":");
					var temp0 = tmpnode.substring(0,index);
					var temp1 = tmpnode.substring(index+1,tmpnode.length);
					html = html + appendHtmp(temp0,temp1);
				}else{
					html = html + appendHtmp('参数',tmpnode);;
				}
			}
		}else{
			html = html + appendHtmp('参数',str);;
		}
		return html;
	}
	
});

//数据表刷新
function reloadLogs(){
	logDataTable.ajax.reload();
}
